<!DOCTYPE html>
<html>
 <head> 
  <meta charset="UTF-8" /> 
  <title>选择权限</title> 
  <link rel="stylesheet" href="../assets/plugins/layui/css/layui.css" media="all" /> 
  <link rel="stylesheet" href="../assets/css/global.css" media="all" /> 
  <style type="text/css">
    .list-row{width: 400px;}
    .name-col{background-color:#FFFFE0; width: 180px;display:inline-block;font-size: 14px;}
    .input-col{width: 100px;display:inline-block;}
    .fixed_btn{
      position:absolute; right:16px; bottom:5px;
    }
  </style> 
 </head> 
 <body> 
  <div class="admin-main"> 
    <form class="layui-form">
       <div id="list_cont" style="height: 450px;overflow-y:auto;"></div>
    </form>
    <div class="fixed_btn"><button id="save_btn" class="layui-btn layui-btn-sm layui-btn-normal">确认</button></div>
  </div> 
  
<script type="text/html" id="list_tpl">
{{# layui.each(d, function(index, item){ }}
<div class="list-row">
    <div class="name-col">{{item.appName}}</div>
    <div class="input-col">
      <input type="radio" name="app_{{item.appId}}" value="RO" title="可查看" {{item.operate === 'RO' ? 'checked' : ''}}>
    </div>
    <div class="input-col">
      <input type="radio" name="app_{{item.appId}}" value="RW" title="可编辑" {{item.operate === 'RW' ? 'checked' : ''}}>
    </div>
</div>
{{# }); }}
</script> 

<script type="text/javascript" src="../assets/plugins/layui/layui.js"></script> 
<script>
layui.config({
	  base: '/assets/js/'
}).use(['form','laytpl','jeesuitelayui'], function(){
  var $ = layui.jquery,form = layui.form,laytpl = layui.laytpl,jeesuitelayui = layui.jeesuitelayui;
  
  var param = {};
  param.userId = jeesuitelayui.getQueryParams('userId');
  param.permissions = new Array();
  
  var url = '/admin/user/user_permission_options?userId=' + param.userId;
  jeesuitelayui.get(url,function(data){
	  var tpl = $('#list_tpl').html();
		laytpl(tpl).render(data, function(html){
			$('#list_cont').html(html);
			form.render('radio');
	 });
  });
  
  $('#save_btn').click(function(){
	  var data = jeesuitelayui.serializeJson($('form'));
	  for(var name in data){
		  var permCode = name.substr(4) + ':' + data[name];
		  param.permissions.push(permCode);
	  }
	  if(param.permissions.length == 0){
		  jeesuitelayui.error('请至少选择一个应用权限');
          return;
	  }
	  jeesuitelayui.post('/admin/user/grant_permissions',param,function(data){
		  jeesuitelayui.success('保存成功');
		  parent.location.reload();
		  setTimeout(function(){
			  var index = parent.layer.getFrameIndex(window.name); 
			  parent.layer.close(index);
		  }, 500);
	  });
  });
  
});  
</script>  
 </body>
</html>